package com.example.nsl.video

import android.app.Dialog
import android.content.Intent
import android.content.pm.ActivityInfo
import android.graphics.Color
import android.media.MediaMetadataRetriever
import android.os.Build
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.text.TextUtils
import android.view.View
import android.view.ViewTreeObserver
import android.view.Window
import android.view.WindowManager
import com.example.nsl.databinding.ActivityQuanpingBinding


class QuanpingActivity : BaseActivity() {

    lateinit var mViewBinding: ActivityQuanpingBinding

    private  var urlPath="https://demo.solveanalysi.com/download/WeChat_20220913092053.mp4"
    private  var currentPosition=0L


    private var mediacontroller:MediaController ?=null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN)

        mViewBinding = ActivityQuanpingBinding.inflate(layoutInflater);
        setContentView(mViewBinding.root)

//        intent?.let {
//            it.getStringExtra("urlPath")?.let { urlPath = it }
//            currentPosition= it.getLongExtra("currentPosition",0L)
//        }

        if(!TextUtils.isEmpty(urlPath)){
            setData(urlPath)
        }else{
            finish()
        }

        mViewBinding.imgSX.setOnClickListener {
            finish()
        }
    }
    private var mVideoCurrantPosition = 0L

    override fun onResume() {
        super.onResume()
        mViewBinding.videoView.seekTo(mVideoCurrantPosition);
    }


    override fun onPause() {
        super.onPause()
        if(mViewBinding.videoView.isPlaying){
            mVideoCurrantPosition =  mViewBinding.videoView.getCurrentPosition();
            mViewBinding.videoView.pause()

        }
    }

    private fun setData(videoUrl:String){
        mViewBinding.run {
//            更新视频宽高数据
            videoView.setVideoPath(videoUrl)
            if(mediacontroller==null){
                mediacontroller=MediaController(this@QuanpingActivity);
                videoView.setMediaController(mediacontroller?.apply {
                    keepScreenOn = true
                })
            }

            videoView.setOnPreparedListener {
//                if (currentPosition != null && currentPosition != 0L) {
//                    videoView.seekTo(currentPosition)
//                }

                handler.postDelayed({
                    if (currentPosition != null && currentPosition != 0L) {
                        videoView.seekTo(currentPosition)
                    }
                },1000L)
            }


            videoView.start()


            // 播放完毕
//            videoView.setOnCompletionListener {
//                it?.let {
//                }
//            }

//            videoView.start()

        }
    }



    var handler=Handler(Looper.getMainLooper())

    override fun onDestroy() {
        handler.removeCallbacksAndMessages(null)
        super.onDestroy()
    }






}